package com.example.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.service.ExcelService;
import com.example.utils.download.DownloadExcelUtil;
import com.example.utils.download.DownloadTemplate;
import com.example.utils.httpServer.HttpServerUtil;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 使用EasyExcel做文件导入导出操作
 * @Author runto
 * @Date 2023/11/3 23:17
 * @PackageName:com.example.controller
 * @ClassName: ExcelController
 * @Version 1.0
 */

@RestController
@RequestMapping("/excel")
public class ExcelController {

    private static final Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    public ExcelService excelService;

    @ApiOperation(value = "模板下载", notes = "excel模板下载-使用预生成文件")
    @RequestMapping(value = "/downloadTemplate", method = RequestMethod.POST)
    public void downloadTemplate(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String reqBody = HttpServerUtil.reader(request, response);
        JSONObject jsonObject = JSONObject.parseObject(reqBody);
        String language = HttpServerUtil.getValue(jsonObject,"language");
        try {
            String realPath = request.getSession().getServletContext().getRealPath("fileTemple");
            String fileName = "downloadTemplate.xlsx";
            logger.info("模板路径:" + realPath);
            String newFileName = DownloadExcelUtil.makerNewExcel(realPath, fileName);
            List<List<String>> strList = new ArrayList<>();
            DownloadExcelUtil.writeExcel(strList, newFileName);
            DownloadTemplate.downLoadFile(request, response, newFileName, language, fileName);
            logger.info("导入---下载---结束,删除文件");
            logger.info("newFileName" + newFileName);
            boolean flag = DownloadExcelUtil.deleteFile(newFileName);
            logger.info("导入---下载---结束,删除文件是否成功：" + flag);
        } catch (Exception e) {
            if (logger.isDebugEnabled()) {
                logger.debug("csyz_error", e);
            }
        }
    }

    @ApiOperation(value = "模板下载", notes = "excel模板下载-使用流导出")
    @RequestMapping(value = "/downloadTemplateForStream", method = RequestMethod.POST)
    public void downloadTemplateForStream(HttpServletRequest request, HttpServletResponse response) {

    }

    /**
     * 导出数据
     * */
    @ApiOperation(value = "导出数据", notes = "导出数据")
    @RequestMapping(value = "/export", method = RequestMethod.GET)
    public void exportInfo(HttpServletResponse response)throws IOException {
        excelService.exportInfo(response);
    }

    /**
     * 导入数据
     * */
    @ApiOperation(value = "导入数据", notes = "导入数据")
    @RequestMapping(value = "/import", method = RequestMethod.POST)
    public void importInfo(@RequestParam("file") MultipartFile file)throws IOException {
        excelService.importInfo(file);
    }

}
